package com.sinothk.special.buyer.userblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinothk.special.buyer.userblog.entity.UserBlogTip;
import com.sinothk.special.buyer.userblog.mapper.UserBlogTipMapper;
import com.sinothk.special.buyer.userblog.service.IUserBlogTipService;
import com.sinothk.special.friendship.entity.FriendshipLink;
import com.sinothk.special.friendship.mapper.FriendshipLinkMapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 博客未读提醒
 * @Author: Liangyt
 * @Date: 2022-06-27
 * @Version: V1.0
 */
@Service
public class UserBlogTipServiceImpl extends ServiceImpl<UserBlogTipMapper, UserBlogTip> implements IUserBlogTipService {

    @Autowired
    private RestTemplate restTemplate;
    @Resource
    private UserBlogTipMapper userBlogTipMapper;
    @Resource
    private FriendshipLinkMapper friendshipLinkMapper;

    @Override
    public void addTip(String blogId, String blogUid) {
        UserBlogTip userBlogTip = new UserBlogTip();
        userBlogTip.setBlogId(blogId);
        userBlogTip.setBlogUid(blogUid);

        LambdaQueryWrapper<FriendshipLink> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FriendshipLink::getFriendUid, blogUid);
        List<FriendshipLink> list = friendshipLinkMapper.selectList(wrapper);

        StringBuilder buffer = new StringBuilder();
        for (FriendshipLink friendshipLink : list) {
            buffer.append(friendshipLink.getMainUid()).append(",");
        }
        userBlogTip.setBlogTipIds(buffer.toString());
        this.save(userBlogTip);

//        Map<String, String> param = new HashMap<>();
//        restTemplate.getForEntity("", Result.class, param);
    }

    @Override
    public void clearAllBlogUnread() {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<UserBlogTip> blogTips = userBlogTipMapper.unreadBlogTips(loginUser.getId());
        for (UserBlogTip blogTip : blogTips) {
            String readerIdsStr = blogTip.getBlogReaderIds();
            blogTip.setBlogReaderIds((readerIdsStr == null ? "" : readerIdsStr) + loginUser.getId() + ",");
        }
        this.updateBatchById(blogTips);
    }
}
